package com.chinasoft.controller;

import com.chinasoft.pojo.*;
import com.chinasoft.service.Impl.UsergoodServiceImpl;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goodlist")
public class UsergoodController {
    @Autowired
    UsergoodServiceImpl usergoodService;
    @PostMapping("/search/{username}")
    public List<Usergood> find(@PathVariable String username) {
        return usergoodService.find(username);
    }

    @PostMapping("/addlist")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody Usergood brand) {
        boolean insertBrand = usergoodService.add(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @PostMapping("/searchupdate/{username}")
    public List<Usergood> findUpdate(@PathVariable String username) {
        return usergoodService.findUpdate(username);
    }

    @PostMapping("/list/count")
    @ResponseBody
    public Result<GoodCount> getDail() {
        List<GoodCount> result = usergoodService.find();
        return new Result(result, "查询成功");
    }
    @PostMapping("/update/{id}")
    public Result updateUse(@PathVariable Integer id) {
        boolean updateUse = usergoodService.updateUse(id);
        if (updateUse) {
            return new Result(null, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }
}
